﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace FDEE.Model
{
    class CPTM1U : CPTMBase
    {
        Dictionary<double, List<int>> dic = new Dictionary<double, List<int>>();
        Dictionary<double, List<int>> addDic = new Dictionary<double, List<int>>();
        internal CPTM1U(string typeName, string pierNumber, eColumnType ct, eSubType st,
            int ww, int ll, int hh, int rr, double up, double low,
            eDB mainDB, string listCString = "", string listAddCString = "")
            : base(typeName, pierNumber, ct,st, ww, ll, hh, rr, up, low, mainDB)
        {
            if ( st == eSubType.C)
            {
                listCString = "{(2,4,6,8,10,12)|90};{(3,11)|225};{(7)|300}";
                listAddCString = "{(3,11)|225};{(2,4,6,8,10,12)|90};{(7)|300}";
                List<string> listC = listCString.Split(';').ToList();
                List<string> listAddC = listAddCString.Split(';').ToList();
                Dictionary<double, List<int>> dicV = new Dictionary<double, List<int>>();

                foreach (var item in listC)
                {
                    string[] values = item.Replace("{", "").Replace("}", "").Replace("(","").Replace(")", "").Replace("（", "").Replace("）", "").Split('|');
                    double vk = double.Parse(values[1]);
                    string[] vs = values[0].Split(',');
                    List<int> v = new List<int>();
                    foreach (var vl in vs)
                    {
                        v.Add(int.Parse(vl));
                    }
                    if (!dicV.Keys.Contains(vk))
                        dicV.Add(vk, v);
                    //dic = dicV.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                    //dic = dicV.OrderBy(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                    dic = dicV;
                }
                Dictionary<double, List<int>> adddicV = new Dictionary<double, List<int>>();
                foreach (var item in listAddC)
                {
                    string[] values = item.Replace("{", "").Replace("}", "").Replace("(","").Replace(")", "").Replace("（", "").Replace("）", "").Split('|');
                    double vk = double.Parse(values[1]);
                    string[] vs = values[0].Split(',');
                    List<int> v = new List<int>();
                    foreach (var vl in vs)
                    {
                        v.Add(int.Parse(vl));
                    }
                    if (!adddicV.Keys.Contains(vk))
                        adddicV.Add(vk, v);
                    //addDic = adddicV.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                    //addDic = adddicV.OrderBy(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
                    addDic = adddicV;
                }
            }
        }

        public override void SetRebarDistribute()
        {
            double d1, d2;
            switch (SubType)
            {
              
                case eSubType.C:
                  
                    d1 = (Length - Radius * 2) / 12;
                    d2 = (Width - Radius * 2) / 7;                 
                    //A截面参数
                    MainLBarDistribute = new RebarTendonDistribute(32, d1, 12);
                    AddLBarDistribute = new RebarTendonDistribute();
                    AddLBarDistribute.Location = MainLBarDistribute.Delete(new List<int>() { 0, 4,6,8,13 });
                    MainWBarDistribute = new RebarTendonDistribute(32, d2, 7);
                    AddWBarDistribute = new RebarTendonDistribute();                    
                    //B截面参数
                    MainLBarDistributeB = new RebarTendonDistribute(32, d1, 12);
                    MainLBarDistributeB.Location = MainLBarDistributeB.Delete(new List<int>() { 2, 6, 8,  });
                    AddLBarDistributeB = new RebarTendonDistribute();
                    MainWBarDistributeB = new RebarTendonDistribute(32, d2, 7);
                    AddWBarDistributeB = new RebarTendonDistribute();
                    //C截面参数
                    MainLBarDistributeC = new RebarTendonDistribute(32, d1, 12);
                    AddLBarDistributeC = new RebarTendonDistribute();
                    MainWBarDistributeC = new RebarTendonDistribute(32, d2, 7);
                    AddWBarDistributeC = new RebarTendonDistribute();
                    break;

            }
        }

        public override void SetRebarList()
        {

            #region 0-箍筋
            int npts = GetStirrupNum();

            RebarList.Add(new MORebarRecord(1, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, (int)MainWBarDistribute.Location[1] * -2 + 32));

            int tmpn = 3;
            int tmp2 = 3;
            switch (SubType)
            {
                case eSubType.C:
                    tmpn = 3;
                    tmp2 = 3;
                    break;
            }

            RebarList.Add(new MORebarRecord(2, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, (int)MainWBarDistribute.Location[tmp2] * -2 + 32));
            RebarList.Add(new MORebarRecord(3, 0, "", eDB.DB12, npts * 3, eShapeType.MO, Width - 100 - 24, MainLBarDistribute.Distance(1, tmpn) + 32));
            RebarList.Add(new MORebarRecord(4, 0, "", eDB.DB12, npts, eShapeType.MO, Length - 100 - 24, Width - 100 - 24));
            #endregion

            #region 1-获取承台尺寸
            FootingBase theFooting = MemberFactory.CreatMember(MemberFactory.FromColumnToFooting(ColumnType), SubType);
            int FTHeight = theFooting.Height;
            int FTWidth = theFooting.Width;
            int FTLen = theFooting.Length;
            int db = (int)MainRebarDB;
            #endregion

            #region 2-起步筋

            RebarList.Add(new MHXRebarRecord(5, 1, "", MainRebarDB, true, 20, eShapeType.MHX));
            RebarList.Add(new MHXRebarRecord(5, 2, "", MainRebarDB, false, 16, eShapeType.MHXe));
            RebarList.Add(new MHXRebarRecord(5, 3, "", MainRebarDB, false, 4, eShapeType.MHXe));
            

            #endregion
            if (SubType == eSubType.C)
            {
                SetTypeCValue();
            }

        }


        private void SetTypeCValue()
        {
            #region 3-锚固筋
            int Z1 = 8494 - (1500 - 180) - 1200; // 搭接节点 1//Z1=8483-(H-160)-1200
            int Z2 = 0;// 搭接点2；
            
            int L2 = 0;// 5.2总长；
            int L3 = 0;// 5.3总长；
            int LtopA = 0;// 5.2筋，墩中长度；
            bool isDiv = false;
            int rr = RebarKnowledge.GerRadius(MainRebarDB);
            if (PierHeight - Z1 + 1200 + 1500 > 12000)
            {
                // 分3节
                isDiv= true;
                int aa = (int)(1300 + LenUpper);
                if (PierHeight-Z1+1200>12000)
                {
                    L3 = 12000; // 优先使用12m定尺的L3;
                    Z2 = Z1 + (12000 - 250 + 248) - 1200*2;
                    L2 = 1500 + (PierHeight - Z2) + 1200;
                }
                else
                {
                    L3 = 9000;
                    Z2 = Z1 + (9000 - 250 + 248) - 1200*2;
                    L2 = 1500 + (PierHeight - Z2) + 1200;
                    if (L2 > 12000)
                    {
                        L2 = 12000;
                        Z2 = PierHeight - (L2 - 1500 - 1200);
                        L3 = Z2 - Z1 + 1300 + 1200;
                    }
                }
                LtopA = L2 - 1500;

                RebarList.Add(new MSZRebarRecord(5, 4, "", MainRebarDB, 40, eShapeType.MSZ, L3 - 2650, 250, 2400));
               
                RebarList.Add(new MLSRebarRecord(5, 1, "a", MainRebarDB, 4, eShapeType.MLS, LtopA, 1500));
                RebarList.Add(new MLZRebarRecord(5, 1, "b", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 316, 1500 - 316 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "c", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 297, 1500 - 297 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "d", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 149, 1500 - 149 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "e", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 149, 1500 - 149 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "f", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 297, 1500 - 297 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "g", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 316, 1500 - 316 - 100));
                RebarList.Add(new MLSRebarRecord(5, 1, "h", MainRebarDB, 6, eShapeType.MLS, LtopA, 1500));
                RebarList.Add(new MLZRebarRecord(5, 1, "i", MainRebarDB, 2, eShapeType.MLZ, LtopA, 100, 180, 1500 - 180 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "j", MainRebarDB, 2, eShapeType.MLZ, LtopA, 100, 180, 1500 - 180 - 100));

                RebarList.Add(new MLRebarRecord(5, 2, "a", MainRebarDB, 2, eShapeType.ML, LtopA, 700, 1500 - 700 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "b", MainRebarDB, 2, eShapeType.ML, LtopA, 640, 1500 - 640 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "c", MainRebarDB, 2, eShapeType.ML, LtopA, 580, 1500 - 580 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "d", MainRebarDB, 2, eShapeType.ML, LtopA, 520, 1500 - 520 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "e", MainRebarDB, 2, eShapeType.ML, LtopA, 460, 1500 - 460 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "f", MainRebarDB, 2, eShapeType.ML, LtopA, 400, 1500 - 400 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "g", MainRebarDB, 2, eShapeType.ML, LtopA, 340, 1500 - 340 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "h", MainRebarDB, 2, eShapeType.ML, LtopA, 280, 1500 - 280 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 3, "a", MainRebarDB, 2, eShapeType.ML, LtopA, 700, 1500 - 700 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 3, "b", MainRebarDB, 2, eShapeType.ML, LtopA, 700, 1500 - 700 + 96, rr));



            }
            else
            {
                // 分2节

                L2 = 1500 + (PierHeight - Z1) + 1200; // 深入盖梁钢筋总长度    
                LtopA = L2 - 1500;
                RebarList.Add(new MLSRebarRecord(5, 1, "a", MainRebarDB, 4, eShapeType.MLS, LtopA, 1500));
                RebarList.Add(new MLZRebarRecord(5, 1, "b", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 316, 1500 - 316 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "c", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 297, 1500 - 297 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "d", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 149, 1500 - 149 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "e", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 149, 1500 - 149 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "f", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 297, 1500 - 297 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "g", MainRebarDB, 1, eShapeType.MLZ, LtopA, 100, 316, 1500 - 316 - 100));
                RebarList.Add(new MLSRebarRecord(5, 1, "h", MainRebarDB, 6, eShapeType.MLS, LtopA, 1500));
                RebarList.Add(new MLZRebarRecord(5, 1, "i", MainRebarDB, 2, eShapeType.MLZ, LtopA, 100, 180, 1500 - 180 - 100));
                RebarList.Add(new MLZRebarRecord(5, 1, "j", MainRebarDB, 2, eShapeType.MLZ, LtopA, 100, 180, 1500 - 180 - 100));
            
                RebarList.Add(new MLRebarRecord(5, 2, "a", MainRebarDB, 2, eShapeType.ML, LtopA, 700, 1500 - 700 + 96,rr));
                RebarList.Add(new MLRebarRecord(5, 2, "b", MainRebarDB, 2, eShapeType.ML, LtopA, 640, 1500 - 640 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "c", MainRebarDB, 2, eShapeType.ML, LtopA, 580, 1500 - 580 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "d", MainRebarDB, 2, eShapeType.ML, LtopA, 520, 1500 - 520 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "e", MainRebarDB, 2, eShapeType.ML, LtopA, 460, 1500 - 460 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "f", MainRebarDB, 2, eShapeType.ML, LtopA, 400, 1500 - 400 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "g", MainRebarDB, 2, eShapeType.ML, LtopA, 340, 1500 - 340 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 2, "h", MainRebarDB, 2, eShapeType.ML, LtopA, 280, 1500 - 280 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 3, "a", MainRebarDB, 2, eShapeType.ML, LtopA, 700, 1500 - 700 + 96, rr));
                RebarList.Add(new MLRebarRecord(5, 3, "b", MainRebarDB, 2, eShapeType.ML, LtopA, 700, 1500 - 700 + 96, rr));
            }

            int NumMainAll = 40;




            RebarList.Add(new MLZRebarRecord(6, 1, "a", MainRebarDB, 2, eShapeType.MLZ, 5450, 100, 180, 1500 - 180 - 100));            
            RebarList.Add(new MLSRebarRecord(6, 1, "b", MainRebarDB, 4, eShapeType.MLS, 5450, 1500));
            RebarList.Add(new MLZRebarRecord(6, 1, "c", MainRebarDB, 2, eShapeType.MLZ, 5450, 100, 180, 1500 - 180 - 100));
            RebarList.Add(new MLZRebarRecord(6, 1, "d", MainRebarDB, 1, eShapeType.MLZ, 5450, 100, 149, 1500 - 149 - 100));
            RebarList.Add(new MLZRebarRecord(6, 1, "e", MainRebarDB, 1, eShapeType.MLZ, 5450, 100, 149, 1500 - 149 - 100));
            RebarList.Add(new MLZRebarRecord(6, 1, "f", MainRebarDB, 1, eShapeType.MLZ, 5450, 100, 297, 1500 - 297 - 100));
            RebarList.Add(new MLZRebarRecord(6, 1, "g", MainRebarDB, 1, eShapeType.MLZ, 5450, 100, 316, 1500 - 316 - 100));
            RebarList.Add(new MLSRebarRecord(6, 1, "h", MainRebarDB, 10, eShapeType.MLS, 5450, 1500));
            RebarList.Add(new MLZRebarRecord(6, 1, "i", MainRebarDB, 2, eShapeType.MLZ, 5450, 100, 180, 1500 - 180 - 100));
            RebarList.Add(new MLZRebarRecord(6, 1, "j", MainRebarDB, 2, eShapeType.MLZ, 5450, 100, 180, 1500 - 180 - 100));
            RebarList.Add(new MHXRebarRecord(6, 1, "k", MainRebarDB, true, 6, eShapeType.MHX, 5972));

            RebarList.Add(new MLRebarRecord(6, 2, "a", MainRebarDB, 2, eShapeType.ML, 5450, 700, 1500 - 700 + 96));
            RebarList.Add(new MLRebarRecord(6, 2, "b", MainRebarDB, 2, eShapeType.ML, 5450, 640, 1500 - 640 + 96));
            RebarList.Add(new MLRebarRecord(6, 2, "c", MainRebarDB, 2, eShapeType.ML, 5450, 580, 1500 - 580 + 96));
            RebarList.Add(new MLRebarRecord(6, 2, "d", MainRebarDB, 2, eShapeType.ML, 5450, 520, 1500 - 520 + 96));

            #endregion

            #region UBlot数量计算
            if (isDiv)
                UAN1N2 = NumMainAll * 8;
            else
                UAN1N2 = NumMainAll * 4;
            if (isDiv)
            {
                UboltList.UBoltNum[MainRebarDB] += NumMainAll * 8;
            }
            else
            {
                UboltList.UBoltNum[MainRebarDB] += NumMainAll * 4;
            }
            #endregion

        }

    }
}
